# Contributor: Josip <bpisoj@gmail.com>

_realname=bsddb3
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=6.1.0
pkgrel=4
pkgdesc="Python bindings for Oracle Berkeley DB (mingw-w64)"
arch=('any')
license=('BSD')
url="https://www.jcea.es/programacion/pybsddb.htm"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-db")
options=('staticlibs' 'strip' '!debug')
source=("https://pypi.python.org/packages/source/b/bsddb3/${_realname}-${pkgver}.tar.gz")
sha256sums=('b13163e3891c32390aea66edcf7880eb245abc89199a766b7b1907fc5c1efd15')

prepare() {
  plain "Patching setup files ..."
  cd "${srcdir}/${_realname}-${pkgver}"
  sed -i -e "s|if os.name == 'posix':|if os.name == 'nt':|g" setup{2,3}.py
  sed -i -e "s|elif os.name == 'nt':|elif os.name == 'FOO':|g" setup{2,3}.py

  cd "${srcdir}"
  cp -r ${_realname}-${pkgver} build-${CARCH}
}

build() {
  CFLAGS+=" -DUNICODE -D_UNICODE"

  export YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION=YES
  export BERKELEYDB_DIR="${MINGW_PREFIX}"

  # Build python 2 module
  cd "${srcdir}/build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-purelib=;--install-data=" \
  ${MINGW_PREFIX}/bin/python2 setup.py build

  # Build python 3 module
  cd "${srcdir}/build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-purelib=;--install-data=" \
  ${MINGW_PREFIX}/bin/python3 setup.py build
}

package_python3-bsddb3() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-db")

  cd "${srcdir}/build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-purelib=;--install-data=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build
}

package_python2-bsddb3() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-db")

  cd "${srcdir}/build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=;--install-purelib=;--install-data=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build
}

package_mingw-w64-i686-python2-bsddb3() {
  package_python2-bsddb3
}

package_mingw-w64-i686-python3-bsddb3() {
  package_python3-bsddb3
}

package_mingw-w64-x86_64-python2-bsddb3() {
  package_python2-bsddb3
}

package_mingw-w64-x86_64-python3-bsddb3() {
  package_python3-bsddb3
}
